var express = require('express');
var router = express.Router();
var utils = require('../../functions/utils');

router.get('/' , function(req,res)
{
	var sessionKey = req.session.key;

	if(utils.isValidString(sessionKey))
	{
		var async = require('async');
		async.waterfall([
			function(cb)
			{
				utils.checkManagerSession(sessionKey , cb);
			}
			,function(result , cb) 
			{
				// 실패 - 로그인 페이지 
				if(result == 0)
				{
					utils.notifyManagerSessionExpired(req , res);
				}
				// 성공 - 실제 작업코드.
				else
				{	
				/////////////////////////////////////////////////////////////////////////////////////////////	

					var fs = require('fs');
					fs.readFile('./template/shareCount' , function(error,data)
					{
						if(error)
						{
							throw error;
						}
						var html = String(data);

						global.replica_db.connection.getConnection(function(err,connection)
						{
							if(err)
							{
								throw err;
							}
						

							var sql = 'call manage_shareCount';

							var query = connection.query(sql, null , function (err, rows) 
							{
								connection.release();
								if(err)
								{
									throw err;
								}

								var content = '';

								for(var i = 0 ; i < rows.length-1 ; i ++)
								{
									var row = '<tr><td>&nbsp;'+rows[i][0].date + '&nbsp;&nbsp;<font color=blue>';

									switch(rows[i][0].weekDay)
									{
										case 1:
											row +='일';
											break;
										case 2:
											row +='월';
											break;
										case 3:
											row +='화';
											break;
										case 4:
											row +='수';
											break;
										case 5:
											row +='목';
											break;
										case 6:
											row +='금';
											break;
										default:
											row +='토';

									}

									row += '&nbsp;</font></td>';

									var start=0;
									var total=0;

									// and  , ios
									for(var os=0;os<2;os++)
									{
										total =0;
										for(var d=start;d<start+8;d++)
										{
											total += rows[i][d].shareCount;
											row += '<td align=right>&nbsp;&nbsp;'+rows[i][d].shareCount+'&nbsp;</td>';
										}

										row += '<td align=right bgcolor=#f7efb7>&nbsp;&nbsp;'+total+'&nbsp;</td>';
										row += '<td align=right bgcolor=#eff6f9>&nbsp;&nbsp;'+rows[i][start+8].shareCount+'&nbsp;</td>';

										start += 9;
									}
									// web
									total = 0;
									total += rows[i][start+2].shareCount;
									row += '<td align=right>&nbsp;&nbsp;'+rows[i][start+2].shareCount+'&nbsp;</td>';
									total += rows[i][start+3].shareCount;
									row += '<td align=right>&nbsp;&nbsp;'+rows[i][start+3].shareCount+'&nbsp;</td>';
									total += rows[i][start+5].shareCount;
									row += '<td align=right>&nbsp;&nbsp;'+rows[i][start+5].shareCount+'&nbsp;</td>';
									row += '<td align=right bgcolor=#f7efb7>&nbsp;&nbsp;'+total+'&nbsp;</td>';

									content += row;

									//일주일 간격 자른다.
									if(rows[i][0].weekDay == 1)
									{
										content += '<tr bgcolor=black><td colspan=25></td></tr>';
									}

								}

								html = html.split("%CONTENT%").join(content); 
								res.writeHead(200 , {'Content-Type' : 'text/html; charset=utf-8'});

								res.end(html);


							}); // query

						}); // getConnection

					}); // readfile
			//////////////////////////////////////////////////////////////////////////////////////////

				} // result==1 
			}
		]); //waterfall
	}
	// 암것도 없으면 loginPage
	else
	{
		utils.gotoManagerLoginPage(req , res);
	}
});

module.exports = router;